<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;


class CommonModel extends Model
{
    // 填充數據
    static public function addData($table,$data){
        return DB::table($table)->insert($data);
    }
    // 填充數據並返回ID
    static public function addDataGetId($table,$data){
        return DB::table($table)->insertGetId($data);
    }
    // 數據+n
    static public function inc($table,$field='',$map=array(),$num=1){
        DB::table($table)->where($map)->increment($field,$num);
    }
    // 數據-n
    static public function dec($table,$field='',$map=array(),$num=1){
        DB::table($table)->where($map)->decrement($field,$num);
    }
    //刪除數據
    static public function delData($table,$map) {
        return DB::table($table)->where($map)->delete();
    }
    // 獲取單行數據
    static public function getOne($table,$map=array(),$field=array()){
        if(!empty($field)){
            return DB::table($table)->select($field)->where($map)->orderBy('id','desc')->first();
        }else{
            return DB::table($table)->where($map)->orderBy('id','desc')->first();
        }
    }
    // 獲取多行行單個字段數據
    static public function getField($table,$field='',$map=array()){
        if(!empty($map)){
            $res = DB::table($table)->where($map)->pluck($field);
            if(count($res)==1){
                return $res[0];
            }elseif(count($res)==0){
                return 0;
            }else{
                return $res;
            }
        }else{
            $res = DB::table($table)->pluck($field);
            return $res;
        }
    }
    // 獲取列表個數
    static public function getCount($table,$map=array()){
        if(!empty($map)){
            return DB::table($table)->where($map)->count();
        }else{
            return DB::table($table)->count();
        }
    }
    
    // 獲取數據列表
    static public function getList($table,$field=array(),$map=array()){
        if(!empty($field)){
            if(!empty($map)){
                return DB::table($table)->select($field)->where($map)->get();
            }else{
                return DB::table($table)->select($field)->get();
            }
        }else{
            if(!empty($map)){
                return DB::table($table)->where($map)->get();
            }else{
                return DB::table($table)->get();
            }
        }
        
    }
    // 獲取分頁數據
    static public function getListPage($table,$count=15,$field=array(),$map=array()){
        if(!empty($field)){
            if(!empty($map)){
                return DB::table($table)->select($field)->where($map)->paginate($count);
            }else{
                return DB::table($table)->select($field)->paginate($count);
            }
        }else{
            if(!empty($map)){
                return DB::table($table)->where($map)->paginate($count);
            }else{
                return DB::table($table)->paginate($count);
            }
        }
        
    }
    // 更新數據
    static public function updateData($table,$map,$data){
        return DB::table($table)->where($map)->update($data);
    }

}
